package com.chris.homecare.components
{
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import feathers.controls.Button;
	import feathers.controls.LayoutGroup;
	import feathers.layout.TiledRowsLayout;
	
	import starling.events.Event;
	
	[Event(name="complete",type="starling.events.Event")]
	public class MenuComponent extends LayoutGroup
	{
		private var eventBtn:Button;
		private var updateBtn:Button;
		private var serviceBtn:Button;
		private var locationBtn:Button;
		private var jobsBtn:Button;
		private var requestBtn:Button;
		
		public function MenuComponent()
		{
			super();
		}
		
		override protected function initialize():void
		{
			const layout:TiledRowsLayout = new TiledRowsLayout();
			layout.horizontalAlign = TiledRowsLayout.HORIZONTAL_ALIGN_LEFT;
			layout.verticalAlign = TiledRowsLayout.VERTICAL_ALIGN_TOP;
			layout.tileHorizontalAlign = TiledRowsLayout.TILE_HORIZONTAL_ALIGN_CENTER;
			layout.tileVerticalAlign = TiledRowsLayout.TILE_VERTICAL_ALIGN_MIDDLE;
			layout.paddingTop = 15;
			layout.gap = 20;
			layout.paddingRight = 15;
			layout.paddingBottom = 10;
			layout.paddingLeft = 15;
			this.layout = layout;
			
			serviceBtn = new Button();
			serviceBtn.nameList.add("services-btn");
			serviceBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			addChild(serviceBtn);
			
			eventBtn = new Button();
			eventBtn.nameList.add("event-btn");
			eventBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			addChild(eventBtn);
			
			updateBtn = new Button();
			updateBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			updateBtn.nameList.add("updates-btn");
			addChild(updateBtn);

			locationBtn = new Button();
			locationBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			locationBtn.nameList.add("location-btn");
			addChild(locationBtn);
			
			jobsBtn = new Button();
			jobsBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			jobsBtn.nameList.add("jobs-btn");
			addChild(jobsBtn);
			
			requestBtn = new Button();
			requestBtn.addEventListener(Event.TRIGGERED, onEventBtnTriggered);
			requestBtn.nameList.add("request-btn");
			addChild(requestBtn);
		}
		
		override protected function draw():void
		{
			super.draw();
			
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			eventBtn.width = eventBtn.height = (stageWidth - 150)/2;
			updateBtn.width = updateBtn.height = (stageWidth - 150)/2;
			serviceBtn.width = serviceBtn.height = (stageWidth - 150)/2;
			locationBtn.width = locationBtn.height = (stageWidth - 150)/2;
			jobsBtn.width = jobsBtn.height = (stageWidth - 150)/2;
			requestBtn.width = requestBtn.height = (stageWidth - 150)/2;
			
			this.width = 50 + (eventBtn.width * 2);
		}
		
		private function onEventBtnTriggered(event:Event):void
		{
			if(Button(event.target).name == "event-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"Event"});
			}

			else if(Button(event.target).name == "services-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"Info", width: this.width-60});
			}
			
			else if(Button(event.target).name == "updates-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"UpdatesList"});
			}
			
			else if(Button(event.target).name == "location-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"Location"});
			}

			else if(Button(event.target).name == "jobs-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"JobList"});
			}

			else if(Button(event.target).name == "request-btn")
			{
				this.dispatchEventWith(Event.COMPLETE,true,{page:"Request"});
			}
			
			CommonUtils.previousScreen = ScreenNames.MENU_SCREEN;
		}
	}
}